#include "threeCoinTrick.h"


//--------------------------------------------------------------
void threeCoinTrick::setup(){
	for (int i = 0; i < 3; i++){
		targets[i].x = ofRandom(0,320);
		targets[i].y = ofRandom(0,240);
		bMoving[i] = false;
		meAsGray[i].allocate(320,240);
		meAnededWithHandGray[i].allocate(320,240);
	}
	bTakeSnapshot = false;
	bHaveCoins = false;
	
	
	//meAsGray[i].allocate(320,240);
}


//--------------------------------------------------------------
void threeCoinTrick::reset(){
	for (int i = 0; i < 3; i++){
		targets[i].x = ofRandom(0,320);
		targets[i].y = ofRandom(0,240);
		bMoving[i] = false;
		bShow[i] = true;
	}
	bTakeSnapshot = false;
	bHaveCoins = false;
	
}


//--------------------------------------------------------------
void threeCoinTrick::update(){
	
	sprintf(name, "three coin");
	
	
	
	for (int i = 0; i < 3; i++){
		meAsGray[i].set(0);
	}
	
	if (bTakeSnapshot == true){
		vbd->takeSnapshot();
		if (vbd->nBlobs > 0){
			for (int i = 0; i < 3; i++){
				memcpy(&coin[i],&vbd->vidBlob, sizeof(videoBlob));
				float xTarget = vbd->vidBlob[0].pos.x + vbd->vidBlob[0].blob.boundingRect.width*(3) + (i)*(vbd->vidBlob[0].blob.boundingRect.width/3.0f + ofRandom(0,10));
				float yTarget = vbd->vidBlob[0].pos.y + (vbd->vidBlob[0].blob.boundingRect.height) * (i-1);
				targets[i].x = xTarget;
				targets[i].y = yTarget;
				targetAngle[i] = ofRandom(-1,1);
				bMoving[i] = false;
				bShow[i] = true;
			}
			bHaveCoins = true;
		}
		bTakeSnapshot = false;
	}
	
	if (bHaveCoins == true){
		for (int i = 0; i < 3; i++){
			if (bMoving[i] == true){
				coin[i].pos.x = 0.95f * coin[i].pos.x + 0.05f * targets[i].x;
				coin[i].pos.y = 0.95f * coin[i].pos.y + 0.05f * targets[i].y;
				coin[i].angle = 0.95f * coin[i].angle + 0.05f * targetAngle[i];
				coin[i].scale = 0.95f * coin[i].scale + 0.05f * 1.0f;
				
			}
			coin[i].update();
			coin[i].draw(meAsGray[i], 255);
			
			float dx = (coin[i].pos.x - targets[i].x);
			float dy = (coin[i].pos.y - targets[i].y);
			
			
			distToTarget[i] = sqrt(dx*dx + dy*dy);
			meAnededWithHandGray[i] = meAsGray[i];
			meAnededWithHandGray[i]  &= vhd->handDetectionImage;
			
			if (distToTarget[i] < 0.8f){
				int meCount = meAsGray[i].countNonZeroInRegion(0,0,width, height);
				int andCount = meAnededWithHandGray[i].countNonZeroInRegion(0,0,width, height);
			
				if (meCount == andCount){
					nFramesCovered[i]++;
					
					if (nFramesCovered[i] > 10){
						bShow[i] = false;
					}
				} else {
					nFramesCovered[i] = 0;
				}
			}
		
		}
		
		
	}
	
	
	
	//---------------------------------------- coins:
}

//--------------------------------------------------------------
void threeCoinTrick::draw(){
	if (bHaveCoins == true){
		for (int i = 0; i < 3; i++){
			if (bShow[i]) coin[i].draw();
		}
	}
	
	for (int i = 0; i < 3; i++){
		//meAnededWithHandGray[i].draw(0,i*240);	
	}
}


//--------------------------------------------------------------
void threeCoinTrick::keyPressed(int key){
	if (key == 's'){
		bTakeSnapshot = true;
	} else if (key == '1') {
		bMoving[0] = true;
	}  else if (key == '2') {
		bMoving[1] = true;
	}else if (key == '3') {
		bMoving[2] = true;
	} else if (key == ' '){
		reset();
	}
}